package com.skyc.servlet;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.skyc.entity.Address;
import com.skyc.entity.User;
import com.skyc.service.AddressService;
import com.skyc.service.ShopService;
import com.skyc.service.UserService;
import com.skyc.service.impl.AddressServiceImpl;
import com.skyc.service.impl.ShopServicImpl;
import com.skyc.service.impl.UserServiceImpl;

/**
 * Servlet implementation class AServlet
 */
@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	UserService us = null;
	PrintWriter out = null;
	HttpSession session = null;
	ShopService ss=null;
    AddressService as=null;
	protected void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
		doPost(req, response);

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ss=new ShopServicImpl();
		session = request.getSession();
		ServletContext application = getServletContext();
		out = response.getWriter();
		String flag = request.getParameter("flag");
		System.out.println(flag);
		us = new UserServiceImpl();
		as=new AddressServiceImpl();
		if (flag != null) {
			switch (flag) {
			case "login":
				doLogin(request, response);
				break;

			case "register":
				doRegister(request, response);

			case "exit":
				doExit(request, response);
			case "resetpwd":
				doresetpwd(request, response);
			default:

				break;
			}

		} else {
			out.print("<script>history.go(-1);</script>");
		}

		out.flush();
		out.close();

	}

	private void doExit(HttpServletRequest request, HttpServletResponse response) {

		session.removeAttribute("user");

		Cookie cName = new Cookie("skyc_login_name", "");
		Cookie cPwd = new Cookie("skyc_login_pwd", "");

		cName.setMaxAge(-1);
		cPwd.setMaxAge(-1);

		response.addCookie(cName);
		response.addCookie(cPwd);

		out.print(session.getAttribute("user") == null ? "1" : "0");
		System.out.println(session.getAttribute("user") == null ? "1" : "0");

	}

	private void doRegister(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String name = request.getParameter("uname");
		String phone = request.getParameter("uphone");
		String pwd = request.getParameter("upwd");
		String yanzheng = request.getParameter("usms");
		if (session.getAttribute("code") == null) {
			session.setAttribute("code", "");
		}
		String code = session.getAttribute("code").toString();

		System.out.println(code);

		if (code.equals(yanzheng)) {
			
			User u = new User();
			u.setUname(name);
			u.setUpwd(pwd);
			u.setUphone(phone);

			User user = us.registerUser(u);
			
			if (user != null) {

				session.setAttribute("user", user);
				//response.sendRedirect("index.jsp");
				out.print("<script>alert('注册成功');</script>");
				request.getRequestDispatcher("toindexServlet").forward(request, response);
				
			} else {
				out.print("<script>alert('手机号已被注册');history.go(-1);</script>");
				
			}
		} else {
			out.print("<script>alert('验证码错误');history.go(-1);</script>");

		}

	}

	private void doresetpwd(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String phone = request.getParameter("uphone");
		String pwd = request.getParameter("resetupwd");
		String yanzheng = request.getParameter("resetusms");
	
		if (session.getAttribute("code") == null) {
			session.setAttribute("code", "");
		}
		String code = session.getAttribute("code").toString();

		if (code.equals(yanzheng)) {	
			User u = new User();
			u.setUphone(phone);
			u.setUpwd(pwd);

			User user = us.resetpwdByphone(u);
			System.out.println(user);
			if (user != null) {
				session.setAttribute("user", user);
				response.sendRedirect("toindexServlet");
				//request.getRequestDispatcher("index.jsp").forward(request, response);
			} else {
				out.print("<script>alert('重置密码失败');history.go(-1);</script>");
			}
		} else {
			out.print("<script>alert('验证码错误');history.go(-1);</script>");

		}

	}

	private void doLogin(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String phone = request.getParameter("uphone");
		String pwd = request.getParameter("upwd");
		User user = us.loginByphone(phone, pwd);
		if (user != null) {
			
			  List<Address> alist=as.QueryByUid(user);
			    session.setAttribute("alist",alist);
			session.setAttribute("user", user);		
			session.setAttribute("shopnum", ss.returnCountByUID(user.getUid()+"")+"");
			String issave = request.getParameter("isSave");
			if ("1".equals(issave)) {
				Cookie cPhone = new Cookie("skyc_login_name", URLEncoder.encode(phone));
				Cookie cPwd = new Cookie("skyc_login_pwd", pwd);

				cPhone.setMaxAge(60 * 60 * 24);
				cPwd.setMaxAge(60 * 60 * 24);
				response.addCookie(cPhone);
				response.addCookie(cPwd);
			}
			
			response.sendRedirect("toindexServlet");
//			request.getRequestDispatcher("index.jsp").forward(request, response);

		} else {
			out.print("<script>alert('输入错误');history.go(-1);</script>");

		}

	}

}
